package org.example.leetCode;

public class longestPalindrome {
    public static void main(String[] args) {
        String s = "cbbd";
        System.out.println(longestPalindromes(s));
    }


    public static String longestPalindromes(String s) {
        if (s==null||s.length()<2){
            return s;
        }
        int resIndex =0;
        int resLength=1;
        for (int idx =0;idx<s.length()-1;idx++){
            int idxLength1 = longestLen(s, idx, idx);
            int idxLength2 = longestLen(s, idx, idx+1);
            if (idxLength1>resLength){
                resLength = idxLength1;
                resIndex = idx - resLength/2;
            }
            if (idxLength2>resLength){
                resLength = idxLength2;
                resIndex = idx - resLength/2+1;
            }
        }
        return s.substring(resIndex,resLength+resIndex);
    }

    private static int longestLen(String s, int left, int right) {
        while (left>=0&&right<s.length()){
            if (s.charAt(left)!=s.charAt(right)){
                break;
            }
            left--;
            right++;
        }
    return right-left-1;
    }
}
